package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.sc.nft.dao.DigitalCollectionProductInfoDao;
import com.sc.nft.dao.ProductStockDao;
import com.sc.nft.dao.PushDetailsDao;
import com.sc.nft.dao.PushRecordDao;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.PushDetails;
import com.sc.nft.entity.PushRecord;
import com.sc.nft.entity.vo.PushDetailsVO;
import com.sc.nft.enums.CommodityType;
import com.sc.nft.enums.PushTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.PushDetailsService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author Sun
 * @since 2022-09-06 14:21:58
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PushDetailsServiceImpl implements PushDetailsService {
    private final PushDetailsDao pushDetailsDao;
    private final PushRecordDao pushRecordDao;
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;
    private final ProductStockDao productStockDao;

    @Override
    public ArrayList<PushDetailsVO> getPushList(Long productId) {
        ArrayList<PushDetailsVO> objectArrayList = Lists.newArrayList();
        List<PushDetails> list = pushDetailsDao.list();
        for (PushDetails pushDetails : list) {
            //如果为空 就是可以推送
            PushRecord record=  pushRecordDao.getListByTypeAndProductId(pushDetails.getPushType(),productId);
            PushDetailsVO pushDetailsVO = BeanUtil.copyProperties(pushDetails, PushDetailsVO.class);
            if(ObjectUtil.isEmpty(record)){
                pushDetailsVO.setIsEnable(Boolean.TRUE);
            }else{
                pushDetailsVO.setIsEnable(Boolean.FALSE);
            }
            objectArrayList.add(pushDetailsVO);
        }
            return objectArrayList;
    }

    @Override
    public void pushProduct(Long productId, ArrayList<PushTypeEnum> list) {
        DigitalCollectionProductInfo productInfo = digitalCollectionProductInfoDao.getById(productId);
        if(productInfo.getCommodityType() != CommodityType.PRODUCT){
            throw new GlobalRunTimeException(ErrorCode.THIS_PRODUCT_TYPE_CANNOT_BE_PUSHED);
        }

        Integer usableStockCount = productStockDao.getUsableStockCount(productId);
        for (PushTypeEnum pushTypeEnum : list) {

            if(pushTypeEnum == PushTypeEnum.HOUSE_OF_TIBETANS){

                insertPushProduct(productInfo,pushTypeEnum,usableStockCount);

            }else if(pushTypeEnum == PushTypeEnum.FAVORITE_APE){

                insertPushProduct(productInfo,pushTypeEnum,usableStockCount);
            }
        }
    }



    private void insertPushProduct(DigitalCollectionProductInfo productInfo, PushTypeEnum pushTypeEnum,Integer usableStockCount){
        PushRecord listByTypeAndProductId = pushRecordDao.getListByTypeAndProductId(pushTypeEnum, productInfo.getId());
        if(ObjectUtil.isNotEmpty(listByTypeAndProductId)){
            throw new GlobalRunTimeException(ErrorCode.DO_NOT_PUSH_REPEATEDLY);
        }

        PushRecord pushRecord = BeanUtil.copyProperties(productInfo, PushRecord.class);
        pushRecord.setProductId(productInfo.getId());
        pushRecord.setCount(usableStockCount);
        pushRecord.setPushType(pushTypeEnum);
        pushRecord.setProductUuid(productInfo.getDigitalCollectionProductInfoUuid());
        pushRecord.insert();
    }
}